package com.fadadianqi.logistics.admin.controller;

import com.fadadianqi.logistics.admin.controller.form.AjaxResult;
import com.fadadianqi.commons.JSONProvider;
import com.fadadianqi.logistics.customer.model.Town;
import com.fadadianqi.logistics.customer.model.Village;
import com.fadadianqi.logistics.customer.service.CustomerService;
import com.yeqiangwei.commons.util.ParamUtils;
import com.yeqiangwei.commons.util.ValidatorHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.MessageSource;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.support.SessionStatus;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.List;
import java.util.Locale;

@Controller
public class VillageController {

    private static final String URI_VILLAGE_LIST = "/a/village/list";

    private static final String VIEW_VILLAGE_LIST = "admin/village/list";

    @RequestMapping(value = "/a/village/form", method = RequestMethod.POST)
    public String save(
            @ModelAttribute("command") Village form, BindingResult result,
            SessionStatus status) {
        form.setOrderby(0);
        customerService.saveVillage(form);
        return "redirect:/a/village/list";
    }

    @RequestMapping(value = "/a/village/form", method = RequestMethod.GET)
    public String showForm(HttpServletRequest request, ModelMap modelMap) {
        String id = ParamUtils.getString(request, "id", null);
        Village village = customerService.getVillageById(id);
        modelMap.put("form", village);
        List<Town> townList = customerService.findAllTown();
        modelMap.put("townList", townList);
        return "admin/village/form";
    }


    @RequestMapping(value = URI_VILLAGE_LIST, method = RequestMethod.GET)
    public String list(HttpServletRequest request, ModelMap modelMap) {
        List<Town> townList = customerService.findAllTown();
        modelMap.put("townList", townList);

        List<VillageVO> voList = new ArrayList<VillageController.VillageVO>();
        for (Town town : townList) {
            VillageVO vo = new VillageVO();
            vo.setTown(town);

            List<Village> villageList = customerService.findAllVillage(town.getId());
            vo.setVillageList(villageList);

            voList.add(vo);
        }
        modelMap.put("villageVOList", voList);
        return VIEW_VILLAGE_LIST;
    }

    @RequestMapping(value = "/a/ajax/village/list", method = RequestMethod.GET)
    public String ajaxList(ModelMap modelMap) {
        List<Town> townList = customerService.findAllTown();
        List<VillageVO> voList = new ArrayList<VillageController.VillageVO>();
        for (Town town : townList) {
            VillageVO vo = new VillageVO();
            vo.setTown(town);

            List<Village> villageList = customerService.findAllVillage(town.getId());
            vo.setVillageList(villageList);

            voList.add(vo);
        }
        modelMap.put("villageVOList", voList);
        return "admin/village/ajaxList";
    }

    @RequestMapping(value = "/a/ajax/village/save", method = RequestMethod.POST)
    @ResponseBody
    public ResponseEntity<String> ajaxSave(
            @ModelAttribute("command") VillageForm form, BindingResult result,
            SessionStatus status) {
        HttpHeaders responseHeaders = new HttpHeaders();
        responseHeaders.add("Content-Type", "text/html; charset=utf-8");
        AjaxResult rs = null;
        if (!ValidatorHelper.isEmpty(form.getTownId())) {
            Village village = new Village();
            if (form.getTownId().equals("-1")) {
                // 没有选择城镇
                if (ValidatorHelper.isNotEmpty(form.getTownName())) {
                    Town town = new Town();
                    town.setOrderby(0);
                    town.setName(form.getTownName());
                    customerService.saveTown(town);
                    village.setTownId(town.getId());
                }
            } else {
                village.setTownId(form.getTownId());
            }
            if (ValidatorHelper.isNotEmpty(village.getTownId())
                    && !village.getTownId().equals("-1")) {
                village.setName(form.getVillageName());
                village.setOrderby(0);
                customerService.saveVillage(village);
                rs = new AjaxResult(JSONProvider.toJSON(village), Boolean.TRUE, null);
            } else {
                String msg = messageSource.getMessage("remind.required.town", null, Locale.getDefault());
                rs = new AjaxResult(null, Boolean.FALSE, msg);
            }
        }
        return new ResponseEntity<String>(JSONProvider.toJSON(rs),
                responseHeaders, HttpStatus.CREATED);
    }

    @RequestMapping(value = "/a/ajax/town/update", method = RequestMethod.POST)
    @ResponseBody
    public ResponseEntity<String> updateTown(
            @ModelAttribute("command") TownVillageAjaxForm form
            , BindingResult result
            , SessionStatus status) {
        HttpHeaders responseHeaders = new HttpHeaders();
        responseHeaders.add("Content-Type", "text/html; charset=utf-8");
        AjaxResult rs = null;
        Town town = customerService.getTownById(form.getId());
        if (town != null) {
            town.setName(form.getName());
            customerService.saveTown(town);
            rs = new AjaxResult(town, Boolean.FALSE, null);
        }
        return new ResponseEntity<String>(JSONProvider.toJSON(rs),
                responseHeaders, HttpStatus.OK);
    }

    @RequestMapping(value = "/a/ajax/village/update", method = RequestMethod.POST)
    @ResponseBody
    public ResponseEntity<String> updateVillage(
            @ModelAttribute("command") TownVillageAjaxForm form
            , BindingResult result
            , SessionStatus status) {
        HttpHeaders responseHeaders = new HttpHeaders();
        responseHeaders.add("Content-Type", "text/html; charset=utf-8");
        AjaxResult rs = null;
        Village village = customerService.getVillageById(form.getId());
        if (village != null) {
            village.setName(form.getName());
            customerService.saveVillage(village);
            rs = new AjaxResult(village, Boolean.FALSE, null);
        }
        return new ResponseEntity<String>(JSONProvider.toJSON(rs),
                responseHeaders, HttpStatus.OK);
    }

    @Autowired
    private CustomerService customerService;

    @Autowired
    private MessageSource messageSource;

    public static class TownVillageAjaxForm {
        private String id;
        private String name;

        public String getId() {
            return id;
        }

        public void setId(String id) {
            this.id = id;
        }

        public String getName() {
            return name;
        }

        public void setName(String name) {
            this.name = name;
        }
    }

    public static class VillageForm {

        private String townId;
        private String villageName;
        private String townName;

        public VillageForm() {

        }

        public String getTownId() {
            return townId;
        }

        public void setTownId(String townId) {
            this.townId = townId;
        }

        public String getVillageName() {
            return villageName;
        }

        public void setVillageName(String villageName) {
            this.villageName = villageName;
        }

        public String getTownName() {
            return townName;
        }

        public void setTownName(String townName) {
            this.townName = townName;
        }

    }

    public static class VillageVO {

        private Town town;

        private List<Village> villageList;

        public Town getTown() {
            return town;
        }

        public void setTown(Town town) {
            this.town = town;
        }

        public List<Village> getVillageList() {
            return villageList;
        }

        public void setVillageList(List<Village> villageList) {
            this.villageList = villageList;
        }
    }
}
